<template>
  <div class="app-container">
    <template>
      <el-row>
        <el-col :span="24">
          <div class="h_top">
            <div class="search_box">
              <el-form :model="query" :inline="true">
                <el-row :gutter="10">
                  <el-col :span="4">
                    <el-form-item>
                      <div>编 号</div>
                      <el-input v-model="query.coding" size="mini" />
                    </el-form-item>
                  </el-col>
                  <el-col :span="4">
                    <el-form-item>
                      <div>名 称</div>
                      <el-input v-model="query.name" size="mini" />
                    </el-form-item>
                  </el-col>
                  <el-col :span="4">
                    <el-form-item>
                      <div>状 态</div>
                      <el-select v-model="query.status" size="mini">
                        <el-option v-for="(item, index) in commonStatus" :key="index" :label="item" :value="index" />
                      </el-select>
                    </el-form-item>
                  </el-col>
                  <el-col :span="4">
                    <el-form-item>
                      <div>上级分类</div>
                      <v-select-s v-model="query.parent_id" action="knowledgeCategoryList" :is-top="true" />
                    </el-form-item>
                  </el-col>
                  <el-col :span="4">
                    <el-form-item>
                      <div>负责人</div>
                      <v-select-s v-model="query.admin_id" />
                    </el-form-item>
                  </el-col>
                  <el-col :span="4">
                    <el-form-item>
                      <div>操 作</div>
                      <el-button type="primary" icon="el-icon-search" size="mini" @click="search">查找</el-button>
                    </el-form-item>
                  </el-col>
                </el-row>
              </el-form>
            </div>
            <div class="toolBars">
              <div class="buttonBox">
                <el-button type="primary" icon="el-icon-document-add" size="mini" @click="addHandler">增加</el-button>
              </div>
              <div class="buttonBox" :hidden="!canButton">
                <el-button type="primary" icon="el-icon-delete" size="mini" :disabled="!selection.length" @click="remove">删除</el-button>
              </div>
              <div class="buttonBox">
                <el-button type="primary" icon="el-icon-refresh" size="mini" @click="reloadHandler">刷新</el-button>
              </div>
              <div class="fr">总数：<span>{{ total }}</span></div>
            </div>
          </div>
        </el-col>
      </el-row>
    </template>
    <div>
      <el-table v-loading="loading" :data="categoryList" size="small" stripe border :header-cell-style="{background:'#eef1f6',color:'#606266'}" style="width: 100%" @selection-change="selectionHandler">
        <el-table-column type="index" label="序号" width="50" align="center" />
        <el-table-column type="selection" align="center" width="45" />
        <el-table-column prop="coding" label="编号" min-width="20%" align="center" />
        <el-table-column prop="name" label="名称" min-width="20%" align="center" />
        <el-table-column prop="status" label="状态" min-width="20%" align="center" :formatter="statusFormat" />
        <el-table-column prop="admin.name" label="维护负责人" min-width="20%" align="center" />
        <el-table-column prop="admin.phone" label="维护负责人" min-width="20%" align="center" />
        <el-table-column fixed="right" label="操作" width="200" align="center">
          <template scope="scope">
            <el-button type="primary" size="mini" @click="updateHandler(scope.row.id)">编辑</el-button>
            <div style="display: inline-block" class="ml10"><div :hidden=!canButton><el-button size="mini" type="danger" @click="confirmDel(scope.row.id)">删除</el-button></div></div>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination class="page" background :page-size="query.limit" layout="total,prev,pager,next,jumper" :total="total" :current-page.sync="query.page" @current-change="index" @size-change="index" />
    </div>
  </div>
</template>
<script>
import API from '@/api/knowledge/category'
import { commonStatus } from '@/utils/options'
import VSelectS from '@/components/select-search'
import { mapGetters } from 'vuex'
export default {
  inject: ['reload', 'goUrl', 'delOne', 'delMany', 'getCommonStatusFormat', 'getScope'],
  components: {
    VSelectS
  },
  data() {
    return {
      loading: false,
      total: 0,
      query: { page: 1, limit: 15 },
      categoryList: [],
      selection: [],
      commonStatus,
      canButton: false
    }
  },
  computed: {
    ...mapGetters([
      'userId'
    ])
  },
  watch: {},
  created() {
    if (this.getScope(this.userId) === 1) {
      this.canButton = true
    }
    const query = JSON.parse(sessionStorage.getItem('query'))
    if (query) {
      this.query = query
      sessionStorage.removeItem('query')
    }
    this.index()
  },
  methods: {
    search() {
      this.query.page = 1
      this.index()
    },
    selectionHandler(selection) {
      this.selection = selection
    },
    typeFormat(row) {
      return this.getTypeFormat(row)
    },
    index() {
      this.loading = true
      API.categoryList(this.query).then(response => {
        this.total = response.data.total
        this.categoryList = response.data.data
      }).finally(() => {
        this.loading = false
      })
    },
    remove() {
      sessionStorage.setItem('query', JSON.stringify(this.query))
      const selection = this.selection
      this.delMany(API, selection)
    },
    confirmDel(id) {
      sessionStorage.setItem('query', JSON.stringify(this.query))
      this.delOne(API, id, false)
    },
    addHandler() {
      this.goUrl('/knowledge/category/category_add')
    },
    updateHandler(id) {
      sessionStorage.setItem('query', JSON.stringify(this.query))
      const url = `/knowledge/category/category_edit/${id}`
      this.goUrl(url)
    },
    reloadHandler() {
      this.reload()
    },
    statusFormat(row) {
      return this.getCommonStatusFormat(row)
    }
  }
}
</script>
